2020-2021 Sunseeker Telemetry and Lighting System
gpio_ex2_inputInterrupt.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*******************************************************************************
33  * MSP430i2xx GPIO - Input Interrupt
34  *
35  * Description: In this example, P1.4 is setup as an output pin. P2.0 is set up
36  * as an input pin that interrupts when the voltage applied goes from high to
37  * low. When the voltage transition happens the ISR is fired and P1.4 is
38  * toggled to show that an interrupt happened. If P2.0 is still low then the
39  * interrupt edge is switched to trigger when P2.0 goes from low to high. This
40  * way an interrupt occurs on every voltage change of P2.0.
41  *
42  *
43  * MSP430i2041
44  * ------------------
45  * /|\| |
46  * | | |
47  * --|RST P2.0|<--Input Signal
48  * | |
49  * | |
50  * | P1.4|-->LED
51  * | |
52  * | |
53  *
54  * Author: Zack Lalanne
55  ******************************************************************************/
56 
57 #include "driverlib.h"
58 
59 int main(void) {
60 
61  WDT_hold(WDT_BASE);
62 
63  // Set P1.4 as output pin to drive the LED
64  GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4);
65 
66  // Setup P2.0 to interrupt on High to Low transition
67  GPIO_setAsInputPin(GPIO_PORT_P2, GPIO_PIN0);
68  GPIO_selectInterruptEdge(GPIO_PORT_P2, GPIO_PIN0,
69  GPIO_HIGH_TO_LOW_TRANSITION);
70  GPIO_clearInterrupt(GPIO_PORT_P2, GPIO_PIN0);
71  GPIO_enableInterrupt(GPIO_PORT_P2, GPIO_PIN0);
72 
73  // Go into LPM0 with interrupts enabled
74  __bis_SR_register(LPM0_bits | GIE);
75 }
76 
77 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
78 #pragma vector=PORT2_VECTOR
79 __interrupt
80 #elif defined(__GNUC__)
81 __attribute__((interrupt(PORT2_VECTOR)))
82 #endif
83 void PORT2_ISR(void) {
84 
85  switch(__even_in_range(P2IV, 0x10)){
86  case 0x00: break; // No interrupt
87  case 0x02: // P2.0 interrupt
88  // Check current state of P2.0 and set correct interrupt edge to occur
89  if(GPIO_getInputPinValue(GPIO_PORT_P2, GPIO_PIN0) == GPIO_INPUT_PIN_HIGH) {
90  GPIO_selectInterruptEdge(GPIO_PORT_P2, GPIO_PIN0,
91  GPIO_HIGH_TO_LOW_TRANSITION);
92  } else {
93  GPIO_selectInterruptEdge(GPIO_PORT_P2, GPIO_PIN0,
94  GPIO_LOW_TO_HIGH_TRANSITION);
95  }
96 
97  // Toggle 1.4 to indicate an interrupt occurred
98  GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN4);
99  break;
100  case 0x04: break; // P2.1 interrupt
101  case 0x06: break; // P2.2 interrupt
102  case 0x08: break; // P2.3 interrupt
103  case 0x0A: break; // P2.4 interrupt
104  case 0x0C: break; // P2.5 interrupt
105  case 0x0E: break; // P2.6 interrupt
106  case 0x10: break; // P2.7 interrupt
107  }
108 }
int main(void)
void PORT2_ISR(void)